// miniprogram/pages/dial/dial.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setting: app.globalData.setting,
    userInfo: app.globalData.userInfo,
    capsule: app.globalData.capsule,
    deg: 0,
    awardGold: 0,
    canRotate: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      setting: app.globalData.setting,
      userInfo: app.globalData.userInfo,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
  },

  getUserInfo() {
    wx.cloud.callFunction({
      name: 'user',
    }).then(res => {
      console.log('userInfo', res.result)
      this.setData({
        userInfo: res.result
      })
    })
  },
  start() {
    let that = this
    let awardDialTimes = this.data.setting.awardDialTimes

    // if (!this.data.canRotate) {
    //   wx.showToast({
    //     title: '手太快了~',
    //     icon:'none'
    //   })
    //   return
    // }
    this.setData({
      canRotate: false,
    })
    wx.cloud.callFunction({
      name: 'dial',
      data: {
        op: 'checkOpenTimes',
      }
    }).then(res => {
      console.log('checkOpenTimes', res)
      if (res.result == 'notEnough') {
        wx.showModal({
          // content: '观看视频可获得 ' + awardDialTimes + ' 次抽奖机会',
          // confirmText: '观看视频',
          // cancelText: '明日再来',
          content: '花300金币，可获得 ' + awardDialTimes + ' 次抽奖机会，博取1万金币大奖。',
          confirmText: '好的',
          cancelText: '明日再来',
          success(res) {
            if (res.confirm) {
              that.addTimes()
       
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
            that.setData({
              canRotate: true,
            })
            return false
          }
        
        })

      } else {
      
        let max = this.data.setting.awards.length
        let rand = 1
        let tempRand = Math.floor(Math.random() * 100)
        if (tempRand == 1) {
          rand = 1
        } else {
          rand = Math.floor(Math.random() * (max - 1)) + 2
        }
        let awardGold = this.data.setting.awards[rand - 1]
        console.log(rand, awardGold)

        this.rotate(rand, this.data.setting.awards)



      }
    })
  },
  rotate(rand, awards) {
    let that = this,
      speed = 30
    let {
      deg,
    } = this.data

    let singleAngle = 360 / awards.length;
    let endAddAngle = 360 - ((rand - 1) * singleAngle + singleAngle / 2) // 中奖角度
    let rangeAngle = (Math.floor(Math.random() * 4) + 4) * 360 // 随机旋转几圈再停止  

    let cAngle
    deg = 0
    let timer = setInterval(() => {
      if (deg < rangeAngle) {
        deg += speed
      } else {
        cAngle = (endAddAngle + rangeAngle - deg) / speed
        cAngle = cAngle > speed ? speed : cAngle < 1 ? 1 : cAngle
        deg += cAngle
        if (deg >= (endAddAngle + rangeAngle)) {
          deg = endAddAngle + rangeAngle
          clearInterval(timer)
          this.updateGold(awards[rand - 1])
          console.log('ok')
        }
      }
      this.setData({
        deg: deg
      })
    }, 1000 / 100)

  },
  updateGold(awardGold) {
    wx.cloud.callFunction({
      name: 'dial',
      data: {
        op: 'updateGold',
        awardGold: awardGold
      }
    }).then(res => {
      console.log('dial', res)
      this.getUserInfo()
      this.setData({
        canRotate: true,
        awardGold: awardGold,
      })

      if (awardGold == 0) {
        wx.showToast({
          title: '手气不佳，再来一次',
          icon: 'none'
        })
      }

    })

  },
  animationend(){
    this.setData({   
      awardGold: 0,
    })
  },
  addTimes(n) {
    wx.cloud.callFunction({
      name: 'dial',
      data: {
        op: 'updateOpenTimes',
      }
    }).then(res => {
      console.log(res)
      this.getUserInfo()
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})